package com.sudichina.sudichina.view.activity.setting;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.view.activity.main.BaseActivity;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.sudichina.sudichina.R.id.et_acpn_confim_phonenumebr;

public class ChangePhoneNumberActivity extends BaseActivity implements onRequestDataFinish {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.et_acpn_input_phonenumebr)
    EditText etAcpnInputPhonenumebr;
    @BindView(et_acpn_confim_phonenumebr)
    EditText etAcpnConfimPhonenumebr;
    @BindView(R.id.et_acpn_identifying_code)
    EditText etAcpnIdentifyingCode;
    @BindView(R.id.bt_get_identifying_code)
    Button btGetIdentifyingCode;
    @BindView(R.id.bt_acpn_confirm)
    Button btAcpnConfirm;
    @BindView(R.id.activity_change_phone_number)
    LinearLayout activityChangePhoneNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone_number);
        ButterKnife.bind(this);
        tvTitle.setText("修改手机号");
    }

    @OnClick({R.id.iv_back, R.id.bt_get_identifying_code, R.id.bt_acpn_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;

            case R.id.bt_get_identifying_code:
                String phone1 = etAcpnInputPhonenumebr.getText().toString().trim();
                String phone2 = etAcpnConfimPhonenumebr.getText().toString().trim();

                if (phone1.length()!=11) {
                    Toastutils.showToast("请输入正确的手机号码");
                    return;
                }

                if (!phone1.equals(phone2)) {
                    Toastutils.showToast("两次输入不一致");
                    return;
                }
                showGetVerificationCodeState();
                sendCode();


                break;
            case R.id.bt_acpn_confirm:

                String phone3 = etAcpnInputPhonenumebr.getText().toString().trim();
                String phone4 = etAcpnConfimPhonenumebr.getText().toString().trim();

                if (phone3.length()!=11) {
                    Toastutils.showToast("请输入正确的手机号码");
                    return;
                }

                if (!phone3.equals(phone4)) {
                    Toastutils.showToast("两次输入不一致");
                    return;
                }
                changeNumber();
                break;
        }
    }

    private void changeNumber() {
        HashMap<String, Object> map = new HashMap<>();
/*
* authTarget: String 手机号/邮箱
authType：String (sms_reg_verify  注册) (sms_forget_verify 忘记密码)
authCode:String 验证码
*
* */
        map.put("authTarget", etAcpnInputPhonenumebr.getText().toString().trim());
        map.put("authType", "sms_reg_verify");
        map.put("authCode", etAcpnIdentifyingCode.getText().toString().trim());
//正常是1
        Type type = new TypeToken<Result>() {
        }.getType();
        NetUtils.requestData(Api.CHANGEPHONENUMBER, map, this, type, false);

    }


    private static final int TIME_INTERVAL = 60;
    private int one_minute = TIME_INTERVAL;
    private Handler handler = new Handler();

    /**
     * 按钮的状态
     */
    public void showGetVerificationCodeState() {
        btGetIdentifyingCode.setEnabled(false);
        handler.post(new Runnable() {
            @Override
            public void run() {
                btGetIdentifyingCode.setText(--one_minute < 0 ? "获取验证码" :
                        String.valueOf(one_minute).concat("秒"));
                if (one_minute < 0) {
                    one_minute = TIME_INTERVAL;
                    btGetIdentifyingCode.setEnabled(true);
                    return;
                }
                handler.postDelayed(this, 1000);
            }
        });
    }

    /**
     * 发送验证码的方法
     */
    private void sendCode() {


        HashMap<String, Object> map = new HashMap<>();
/*
"authTarget":"15321859523",
"authType":"sms_reg_verify",
authSource：String 来源 IOS ，Android，web
 */
        map.put("authTarget", etAcpnInputPhonenumebr.getText().toString().trim());
        map.put("authType", "sms_reg_verify");
        map.put("authSource", "Android");
        //map.put("authCode", etAcpnIdentifyingCode.getText().toString().trim());

        Type type = new TypeToken<Result>() {
        }.getType();


        NetUtils.requestData(Api.SEND_CODE, map, this, type, false);
    }

    @Override
    public void requestdataFinish(String catalog, Object bean) {

        switch (catalog) {


            case Api.SEND_CODE:
                Result bean1 = (Result) bean;
                int code = bean1.getCode();
                if (code == 1) {
                    Toastutils.showToast("发送成功");
                }


                break;

            case Api.CHANGEPHONENUMBER:
                Result bean2 = (Result) bean;
                System.out.println(bean2.toString()+"这是一个悲伤的故事");
//                int code2 = bean2.getCode();
//                if (code2 == 1) {
//                    Toastutils.showToast("修改成功");
//                    finish();
//                }else if(code2 == 8007){
//                    Toastutils.showToast("验证码失效");
//                }
//正常是1


                break;


        }


    }

    @Override
    public void requestdataFailed(IOException e) {

    }
}
